package com.xneure.enging;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import com.xneure.utils.intf.SystemListener;

public class SessionListener implements SystemListener{
	/**
	 * 定义存储客户登录session的集合.
	 */
	private static List<Object> sessions = new ArrayList<Object>();

	/**
	 * session覆盖时的监听方法.
	 * 
	 * @param HttpSessionBindingEvent
	 *            session事件
	 */
	public void attributeReplaced(HttpSessionBindingEvent sbe) {
	}

	public void sessionCreated(HttpSessionEvent ses) {
		
	}

	public void sessionDestroyed(HttpSessionEvent ses) {
		RequestContext.destroyNotifyEvent(ses.getSession().getId());
	}
	/**
	 * 返回客户登录session的集合.
	 * 
	 * @return
	 */
	public static List<Object> getSessions() {
		return sessions;
	}

	public static int getMemberSum() {
		return sessions.size();
	}

	public static Boolean isExistInSessions(String username) {
		String user_name;
		for (Integer i = 0; i < sessions.size(); i++) {
			user_name = (String) sessions.get(i);
			if (user_name.equals(username)) {
				return true;
			} else {
				continue;
			}
		}
		return false;
	}

	// 此方法在初始化监听器的时候会调用，用于spring注入
	@Override
	public void start(ServletContextEvent sce){
	}

	public void stop(ServletContextEvent sce) {
		
	}

}
